/**
 * 
 */
package org.mspring.mlog.service.security.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.mspring.mlog.entity.security.Resource;
import org.mspring.mlog.entity.security.Role;
import org.mspring.mlog.entity.security.RoleResource;
import org.mspring.mlog.entity.security.RoleResourcePK;
import org.mspring.mlog.service.security.ResourceSecurityService;
import org.mspring.platform.dao.hibernate.GenericDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Gao Youbo
 * @since 2013-2-17
 * @description
 * @TODO
 */
@Service
@Transactional
@SuppressWarnings("unchecked")
public class ResourceSecurityServiceImpl extends GenericDao implements ResourceSecurityService {

    private static final Logger log = Logger.getLogger(ResourceSecurityServiceImpl.class);

    @Override
    public List<Resource> findResourceByRole(Long roleId) {
        // TODO Auto-generated method stub
        return getHibernateTemplate().list("select roleResource.PK.resource from RoleResource roleResource where roleResource.PK.role.id = ?", roleId);
    }

    @Override
    public List<Resource> findResourceByUser(Long userId) {
        // TODO Auto-generated method stub
        return getHibernateTemplate().list("select roleResource.PK.resource from RoleResource roleResource, UserRole userRole where userRole.PK.role.id = roleResource.PK.role.id and userRole.PK.user.id = ?", userId);
    }

    @Override
    public boolean hasUserResourcePermission(Long userId, Long resourceId) {
        // TODO Auto-generated method stub
        int count = getHibernateTemplate().count("select count(*) from RoleResource roleResource, UserRole userRole where userRole.PK.role.id = roleResource.PK.role.id and userRole.PK.user.id = ? and roleResource.PK.resource.id = ?", new Long[] { userId, resourceId });
        return count > 0;
    }

    @Override
    public List<RoleResource> findRoleResourceByRole(Long roleId) {
        // TODO Auto-generated method stub
        return getHibernateTemplate().list("select roleResource from RoleResource roleResource where roleResource.PK.role.id = ?", roleId);
    }

    @Override
    public void createRoleResource(Long role, String... resources) {
        // TODO Auto-generated method stub
        RoleResourcePK pK = new RoleResourcePK();
        pK.setRole(new Role(role));
        for (String resource : resources) {
            try {
                pK.setResource(new Resource(new Long(resource)));
                RoleResource roleResource = new RoleResource(pK);
                getHibernateTemplate().merge(roleResource);
            } catch (Exception e) {
                // TODO: handle exception
                log.error("create role-resource failure! role = " + role + ", resource = " + resource);
                continue;
            }
        }
    }

    @Override
    public void removeRoleResource(Long role, String... resources) {
        // TODO Auto-generated method stub
        for (String resource : resources) {
            try {
                getHibernateTemplate().executeUpdate("delete RoleResource rr where rr.PK.role.id = ? and rr.PK.resource.id = ?", new Object[] { role, new Long(resource) });
            } catch (Exception e) {
                // TODO: handle exception
                log.error("remove role-resource failure! role = " + role + ", resource = " + resource, e);
                continue;
            }
        }
    }

}
